package com.ambulance.project.biz.controller.pad;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.entity.Nhiss;
import com.ambulance.project.biz.entity.NhissAnswer;
import com.ambulance.project.biz.service.NhissAnswerService;
import com.ambulance.project.biz.service.NhissService;
import com.ambulance.utils.ChildUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;


@Api(tags = {"随车医生-NHISS相关接口"})
@RequestMapping(value = "/pad/v1/nhiss", method = RequestMethod.POST)
@Controller
public class NhissController {

    @Autowired
    private NhissService nhissService;
    @Autowired
    private NhissAnswerService nhissAnswerService;

    @RequestMapping(value = "/add")
    @ResponseBody
    @ApiOperation(value = "添加Nhiss", notes = "添加Nhiss", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number", value = "编号/分", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "value", value = "题干/答案", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "父id", required = false, paramType = "form", dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    public Object add(
            @RequestParam(value = "number") String number,
            @RequestParam(value = "value") String value,
            @RequestParam(value = "pid", required = false, defaultValue = "0") Integer pid
    ) {
        final Nhiss nhiss = new Nhiss();
        nhiss.setNumber(number);
        nhiss.setValue(value);
        nhiss.setPid(pid);
        final boolean save = nhissService.save(nhiss);
        return AjaxResult.AutoResponse(save);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "查询Nhiss信息", notes = "查询Nhiss信息", produces = "application/json")
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数不正确"),
            @ApiResponse(code = 404, message = "404请求路径不正确"),
            @ApiResponse(code = 200, response = Nhiss.class, message = "响应")
    })
    public Object list() {
        final List<Nhiss> list = nhissService.list();
        return AjaxResult.success(ChildUtils.getNhissChild(list));
    }

//    @RequestMapping(value = "/delete")
//    @ResponseBody
//    @ApiOperation(value = "删除Nhiss", notes = "删除Nhiss", produces = "application/json")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "Nhiss编号", required = true, paramType = "form", dataType = "int")
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
//    })
//    public Object delete(@RequestParam(value = "id") Integer id
//    ) {
//        boolean b = nhissService.removeById(id);
//        return AjaxResult.AutoResponse(b);
//    }

//    @RequestMapping(value = "/update")
//    @ResponseBody
//    @ApiOperation(value = "更新NHISS信息", notes = "更新NHISS信息", produces = "application/json")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "角色编号", required = true, paramType = "form", dataType = "int"),
//            @ApiImplicitParam(name = "number", value = "编号/分", required = false, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "value", value = "题干/答案", required = false, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "pid", value = "父级id", required = false, paramType = "form", dataType = "int"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
//    })
//    public Object update(
//            @RequestParam(value = "id") Integer id,
//            @RequestParam(value = "number", required = false) String number,
//            @RequestParam(value = "value", required = false) String value,
//            @RequestParam(value = "pid", required = false) Integer pid
//    ) {
//        final Nhiss nhiss = new Nhiss();
//        nhiss.setId(id);
//        nhiss.setNumber(number);
//        nhiss.setValue(value);
//        nhiss.setPid(pid);
//        try {
//            boolean b = nhissService.updateById(nhiss);
//            return AjaxResult.AutoResponse(b);
//        } catch (Exception e) {
//            throw new CustomException("系统繁忙");
//        }
//    }

    @RequestMapping(value = "/get")
    @ResponseBody
    @ApiOperation(value = "获取NHISS信息", notes = "获取NHISS信息", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dispatchCaseID", value = "调度病例编号", required = true, paramType = "form", dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    public Object get(
            @RequestParam(value = "dispatchCaseID") Integer dispatchCaseID
    ) {
        final QueryWrapper<NhissAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dcid", dispatchCaseID);
        final NhissAnswer nhissAnswerServiceOne = nhissAnswerService.getOne(queryWrapper);
        String content = nhissAnswerServiceOne.getContent();
        JSONObject jsonObject = JSON.parseObject(content);
        final List<Nhiss> list = ChildUtils.getNhissChild(nhissService.list());
        for (Nhiss nhiss : list) {
            nhiss.setAnswer(jsonObject.getInteger(String.valueOf(nhiss.getNumber())));
        }
        final HashMap<String, Object> map = Maps.newHashMap();
        map.put("data", list);
        map.put("sum", nhissAnswerServiceOne.getSum());
        return AjaxResult.success(map);
    }


}
